<template>
    <div class="background">
        <div class="more-book-container">
            <!-- 搜索部分 -->
            <div class="search-section">
                <div class="search-container">
                    <el-select v-model="searchType" class="search-type-select" popper-class="search-type-dropdown">
                        <el-option label="书名" value="title" />
                        <el-option label="作者" value="author" />
                    </el-select>
                    <el-input v-model="searchQuery" placeholder="请输入搜索内容" class="search-input"
                        @keyup.enter="handleSearch">
                        <template #append>
                            <el-button :icon="Search" @click="handleSearch" />
                        </template>
                    </el-input>
                </div>
            </div>

            <!-- 书籍卡片部分 -->
            <div class="books-section">
                <div class="books-grid">
                    <div v-for="book in displayedBooks" :key="book.id" class="book-card" @click="openBookDetail(book)">
                        <img :src="book.cover" :alt="book.title" class="book-cover" />
                        <div class="book-info">
                            <h3 class="book-title">{{ book.title }}</h3>
                            <p class="book-author">{{ book.author }}</p>
                            <p class="book-description">{{ book.description }}</p>
                            <p class="book-date">{{ formatDate(book.publishDate) }}</p>
                        </div>
                    </div>
                </div>

                <!-- 加载更多 -->
                <div class="load-more">
                    <el-button v-if="hasMoreBooks" @click="loadMore" class="load-more-btn">
                        加载更多
                    </el-button>
                    <p v-else class="no-more">没有更多书籍了</p>
                </div>
            </div>
        </div>
        <bookDetail v-model="detailVisible" :book="selectedBook" />
    </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { Search } from '@element-plus/icons-vue'
import bookDetail from './bookDetail.vue'

interface Book {
    id: number
    title: string
    cover: string
    description: string
    author: string
    publishDate: Date
}

// 详情弹窗控制
const detailVisible = ref(false)
const selectedBook = ref<Book | null>(null)

// 打开书籍详情
const openBookDetail = (book: Book) => {
    selectedBook.value = book
    detailVisible.value = true
}

// 搜索类型和查询
const searchType = ref('title')
const searchQuery = ref('')

// 所有书籍数据
const allBooks = ref<Book[]>([])
// 当前显示的书籍数量
const displayedCount = ref(9)

// 生成模拟数据
const generateBooks = () => {
    const books: Book[] = []
    const titles = [
        '诗经解析', '楚辞全注', '论语新解', '孟子正义', '道德经释义',
        '庄子浅注', '史记选读', '汉书精要', '三国志评注', '唐诗三百首',
        '宋词精选', '元曲鉴赏', '明清小说研究', '红楼梦探秘', '水浒传评析',
        '西游记解读', '三国演义研究', '古文观止', '资治通鉴选', '二十四史概要'
    ]

    const authors = [
        '王教授', '李博士', '张研究员', '刘学者', '陈专家',
        '赵大家', '钱先生', '孙老师', '周院士', '吴作家'
    ]

    const descriptions = [
        '本书深入解析了中国古典文学精髓，适合所有文学爱好者阅读。',
        '这是一部系统研究传统文化的重要著作，具有很高的学术价值。',
        '作者通过多年研究，提出了许多创新观点，令人耳目一新。',
        '本书资料翔实，论证充分，是相关领域不可多得的参考书。',
        '图文并茂，深入浅出，适合初学者和专业人士阅读。',
        '系统梳理了历史文化发展脉络，有助于读者建立整体认知。',
        '结合最新考古发现，对传统观点进行了修正和补充。',
        '从多学科角度进行交叉研究，提供了全新的解读视角。'
    ]

    for (let i = 1; i <= 45; i++) {
        const randomTitle = titles[Math.floor(Math.random() * titles.length)]
        const randomAuthor = authors[Math.floor(Math.random() * authors.length)]
        const randomDesc = descriptions[Math.floor(Math.random() * descriptions.length)]

        books.push({
            id: i,
            title: `${randomTitle} ${i}`,
            cover: `https://dummyimage.com/120x160/5a3e2f/ffffff&text=${encodeURIComponent(randomTitle)}`,
            description: randomDesc,
            author: randomAuthor,
            publishDate: new Date(2010 + Math.floor(i / 5), i % 12, (i % 28) + 1)
        })
    }

    return books
}

// 过滤后的书籍
const filteredBooks = computed(() => {
    if (!searchQuery.value) return allBooks.value

    return allBooks.value.filter(book => {
        if (searchType.value === 'title') {
            return book.title.toLowerCase().includes(searchQuery.value.toLowerCase())
        } else {
            return book.author.toLowerCase().includes(searchQuery.value.toLowerCase())
        }
    })
})

// 当前显示的书籍
const displayedBooks = computed(() => {
    return filteredBooks.value.slice(0, displayedCount.value)
})

// 是否还有更多书籍
const hasMoreBooks = computed(() => {
    return displayedCount.value < filteredBooks.value.length
})

// 处理搜索
const handleSearch = () => {
    displayedCount.value = 9
}

// 加载更多书籍
const loadMore = () => {
    displayedCount.value += 9
}

// 格式化日期
const formatDate = (date: Date): string => {
    return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`
}

// 初始化数据
onMounted(() => {
    allBooks.value = generateBooks()
})
</script>

<style scoped>
.background {
    background-color: #f5efe6;
}

.more-book-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 30px 20px;
    font-family: 'Noto Sans SC', sans-serif;
    color: #5a3e2f;
    background-color: #f5efe6;
    min-height: 100vh;
}

/* 搜索部分 */
.search-section {
    display: flex;
    justify-content: center;
    margin-bottom: 40px;
}

.search-container {
    display: flex;
    width: 100%;
    max-width: 600px;
    box-shadow: 0 4px 12px rgba(90, 62, 47, 0.1);
    border-radius: 6px;
    overflow: hidden;
}

.search-type-select {
    width: 120px;

    :deep(.el-input__inner) {
        border-radius: 6px 0 0 6px;
        border-right: none;
    }
}

.search-input {
    flex: 1;

    :deep(.el-input-group__append) {
        background-color: #5a3e2f;
        color: #f5efe6;
        border: none;
        border-radius: 0 6px 6px 0;

        .el-button {
            color: #f5efe6;
        }
    }
}

/* 书籍网格 */
.books-section {
    width: 100%;
}

.books-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 25px;
    margin-bottom: 40px;
}

/* 书籍卡片 */
.book-card {
    display: flex;
    background-color: #fffaf3;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 4px 12px rgba(90, 62, 47, 0.08);
    transition: all 0.3s ease;
    height: 200px;
}

.book-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 20px rgba(90, 62, 47, 0.15);
}

.book-cover {
    width: 120px;
    height: 100%;
    object-fit: cover;
    flex-shrink: 0;
}

.book-info {
    padding: 20px;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

.book-title {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 8px;
    color: #5a3e2f;
}

.book-author {
    font-size: 14px;
    color: #8b6152;
    margin-bottom: 10px;
}

.book-description {
    font-size: 14px;
    line-height: 1.5;
    color: #5a3e2f;
    margin-bottom: 10px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.book-date {
    font-size: 12px;
    color: #7a9aae;
    margin-top: auto;
}

/* 加载更多 */
.load-more {
    display: flex;
    justify-content: center;
    margin-top: 30px;
}

.load-more-btn {
    background-color: #5a3e2f;
    color: #f5efe6;
    border: none;
    padding: 12px 30px;
    border-radius: 6px;
    font-weight: 500;
    transition: all 0.3s ease;
}

.load-more-btn:hover {
    background-color: #8b6152;
    transform: translateY(-2px);
}

.no-more {
    text-align: center;
    color: #7a9aae;
    font-style: italic;
}

/* 响应式设计 */
@media (max-width: 968px) {
    .books-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (max-width: 640px) {
    .books-grid {
        grid-template-columns: 1fr;
    }

    .search-container {
        flex-direction: column;
    }

    .search-type-select {
        width: 100%;
        margin-bottom: 10px;

        :deep(.el-input__inner) {
            border-radius: 6px;
            border-right: 1px solid #dcdfe6;
        }
    }

    .book-card {
        flex-direction: column;
        height: auto;
    }

    .book-cover {
        width: 100%;
        height: 200px;
    }
}

/* 全局样式覆盖以匹配主题 */
.search-type-dropdown .el-select-dropdown__item {
    color: #5a3e2f;
}

.search-type-dropdown .el-select-dropdown__item.selected {
    color: #5a3e2f;
    background-color: #f5efe6;
}

.search-type-dropdown .el-select-dropdown__item:hover {
    background-color: #e4d8c8;
}
</style>